package com.vimi8.chick.activity;

import android.content.Intent;
import android.widget.Toast;

import com.eros.framework.manager.ManagerFactory;
import com.eros.framework.manager.impl.dispatcher.DispatchEventManager;
import com.eros.framework.model.UploadResultBean;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.umeng.socialize.UMShareAPI;
import com.vimi8.chick.plugins.constant.Constant;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends com.eros.framework.activity.MainActivity {

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }

        if (data != null) {
            switch (requestCode) {
                // 拍照
                case Constant.PictureConstants.PICTURE_CAMERA:
                    List<LocalMedia> cameraItems = PictureSelector.obtainMultipleResult(data);
                    if (cameraItems.size() > 0) {
                        LocalMedia item = cameraItems.get(0);
                        cameraResult(getMediaPath(item));
                    }
                    break;

                // 视频
                // case Constant.PictureConstants.PICTURE_VIDEO:
                //     videoResult(data.getStringExtra("video_record_path"));
                //     break;
                case Constant.PictureConstants.PICTURE_VIDEO:
                    int type = data.getIntExtra(Constant.PictureConstants.VIDEO_RECORD_TYPE, Constant.PictureConstants.VIDEO_RECORD_TYPE_IMAGE);
                    String image = data.getStringExtra(Constant.PictureConstants.VIDEO_RECORD_IMAGE);
                    if (type == Constant.PictureConstants.VIDEO_RECORD_TYPE_IMAGE) {
                        List<String> list = new ArrayList<>();
                        list.add(image);
                        pickResult(list);
                    } else {
                        String videoPath = data.getStringExtra(Constant.PictureConstants.VIDEO_RECORD_PATH);
                        videoResult(videoPath, image);
                    }
                    break;

                // 选择照片
                case Constant.PictureConstants.PICTURE_PICKER:
                    List<LocalMedia> pictureItems = PictureSelector.obtainMultipleResult(data);
                    pickResult(getMediaPaths(pictureItems));
                    break;

                case Constant.PermissionConstants.REQUEST_CODE_APP_INSTALL:
                    Toast.makeText(this, "权限申请成功，请重新进行更新", Toast.LENGTH_SHORT).show();
            }
        }


        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    private List<String> getMediaPaths(List<LocalMedia> items) {
        List<String> list = new ArrayList<>();
        for (LocalMedia item : items) {
            list.add(getMediaPath(item));
        }
        return list;
    }

    private String getMediaPath(LocalMedia media) {
        return media.isCut() ? media.getCutPath() : (media.isCompressed() ? media.getCompressPath() : media.getPath());
    }

    private void cameraResult(String path) {
        UploadResultBean uploadResultBean = new UploadResultBean();
        uploadResultBean.resCode = 0;
        uploadResultBean.msg = "拍照成功";
        List<String> dataList = new ArrayList<>();
        dataList.add(path);
        uploadResultBean.setData(dataList);
        ManagerFactory.getManagerService(DispatchEventManager.class).getBus().post(uploadResultBean);
    }

    /**
     * 视频拍摄完成读取结果
     */
    //    private void videoResult(String path) {
    //        UploadResultBean uploadResultBean = new UploadResultBean();
    //        uploadResultBean.resCode = 0;
    //        uploadResultBean.msg = "视频拍摄成功";
    //
    //        List<String> dataList = new ArrayList<>();
    //        dataList.add(path);
    //
    //        // 获取视频缩略图（临时文件）
    //        Bitmap bitmap = FileUtil.INSTANCE.getVideoThumbnail(path);
    //        String thumbFilePath = new File(
    //            FileManager.getTempFilePath(this),
    //            String.valueOf(SystemClock.currentThreadTimeMillis())
    //        ).getAbsolutePath() + ".jpg";
    //        String thumbFileUrl = ImageUtil.zoomImage(this, bitmap, 800, BIGGESTWIDTH, thumbFilePath);
    //        dataList.add(thumbFileUrl);
    //
    //        uploadResultBean.setData(dataList);
    //        ManagerFactory.getManagerService(DispatchEventManager.class).getBus().post(uploadResultBean);
    //    }
    private void videoResult(String videoPath, String thumbPath) {
        UploadResultBean uploadResultBean = new UploadResultBean();
        uploadResultBean.resCode = 0;
        uploadResultBean.msg = "视频拍摄成功";
        List<String> dataList = new ArrayList<>();
        dataList.add(videoPath);
        dataList.add(thumbPath);
        uploadResultBean.setData(dataList);
        ManagerFactory.getManagerService(DispatchEventManager.class).getBus().post(uploadResultBean);
    }

    private void pickResult(List<String> items) {
        UploadResultBean uploadResultBean = new UploadResultBean();
        uploadResultBean.resCode = 0;
        uploadResultBean.msg = "图片选择成功";
        uploadResultBean.setData(items);
        ManagerFactory.getManagerService(DispatchEventManager.class).getBus().post(uploadResultBean);
    }

    @Override
    protected void onDestroy() {
        closeDialog();
        super.onDestroy();
    }
}
